package priv.xp.androidexample.repository

import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.HttpException
import com.github.kittinunf.fuel.coroutines.awaitObject
import com.google.gson.JsonParseException
import priv.xp.androidexample.model.GankDeserializer
import priv.xp.androidexample.model.Girl

class GirlRepository {
    suspend fun awaitLoadPage(page: Int): List<Girl> {
        try {
            val gank = Fuel.get("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/$page")
                .awaitObject(GankDeserializer)

            if (!gank.error) {
                return gank.results
            }
        } catch (exception: Exception) {
            when (exception) {
                is HttpException -> println("A network request exception was thrown: ${exception.message}")
                is JsonParseException -> println("A serialization/deserialization exception was thrown: ${exception.message}")
                else -> println("An exception [${exception.javaClass.simpleName}\"] was thrown")
            }
        }

        return arrayListOf()
    }
}